package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.DiscountLevel;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDiscountLevelService;
import cn.wolfcode.crm.service.IMessageHistoryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("discountLevel")
public class DiscountLevelController {
    @Autowired
    private IDiscountLevelService discountLevelService;

    @Autowired
    private IMessageHistoryService messageHistoryService;

    @RequestMapping("")
    public String index() {
        return "settings/settings";
    }

    @RequestMapping("message")
    public String message() {
        return "settings/message";
    }


    @RequiresPermissions("discountLevel:view")
    @PermissionName("会员折扣列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo) {
        PageResult query = discountLevelService.query(qo);
        return query;
    }


    @RequestMapping("queryAllDiscountLevel")
    @ResponseBody
    //查询全部会员折扣  
    public List<DiscountLevel> queryAllDiscountLevel() {
        return discountLevelService.list();
    }


    @RequiresPermissions("discountLevel:save")
    @PermissionName("会员折扣保存")
    @RequestMapping("save")
    public String save(DiscountLevel entity) {
        if (entity.getEntitys().size() != 0) {
            List<DiscountLevel> entitys = entity.getEntitys();
            for (DiscountLevel ent : entitys) {
                if (ent != null && ent.getDiscount() != null && ent.getMaxtotalpoint() != null && ent.getMintotalpoint() != null) {
                    System.out.println(ent.toString());
                    if (ent.getId() != null) {
                        discountLevelService.update(ent);
                    } else {
                        discountLevelService.save(ent);
                    }
                }

            }
        }
        return "redirect:/discountLevel";
    }
    @RequiresPermissions("discountLevel:saveMessage")
    @PermissionName("发送短信保存")
    @RequestMapping("saveMessage")
    public String saveMessage(String message) {
        discountLevelService.saveMessage(message);
        return "redirect:/discountLevel";
    }

    @RequiresPermissions("discountLevel:update")
    @PermissionName("编辑会员折扣")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(DiscountLevel entity) {
        JsonResult result = new JsonResult();
        try {
            discountLevelService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("编辑信息失败");
        }
        return result;
    }

    @RequiresPermissions("discountLevel:delete")
    @PermissionName("删除会员折扣")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            discountLevelService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("删除信息失败");
        }
        return result;
    }


}
